<?php include('../includes/header.php'); ?>
<?php include('../includes/mysqli_connect.php'); ?>
<?php include('../includes/functions.php'); ?>
<?php include('../includes/sidebar-admin.php'); ?>

<div id="content">
    <h2>Manage Pages</h2>
    <table>
        <thead>
            <tr>
                <th><a href="view_pages.php?sort=page">Pages</a></th>
                <th><a href="view_pages.php?sort=on">Posted on</a></th>
                <th><a href="view_pages.php?sort=by">Posted by</a></th>
                <th>Content</th>
                <th>Edit</th>
                <th>Delete</th>
            </tr>
        </thead>        
        <tbody>
            <?php
            //sap xep theo thu tu table head
            if (isset($_GET['sort'])) {
                switch ($_GET['sort']) {
                    case 'page':
                        $order_by = 'page_name';
                        break;
                    case 'on':
                        $order_by = 'date';
                        break;
                    case 'by':
                        $order_by = 'name';
                        break;

                    default:
                        $order_by = 'date';
                        break;
                }
            } else {
                $order_by = 'date';
            }
            //truy xuat co so du lieu
            $q = "SELECT p.page_id,p.page_name,DATE_FORMAT(p.post_on, '%b %e %Y') AS date, CONCAT_WS(' ', first_name, last_name) AS name, p.content ";
            $q.=" FROM pages AS p ";
            $q.= " JOIN users AS u ";
            $q.=" USING(user_id) ";
            $q.=" ORDER BY {$order_by} ASC";
            $r = mysqli_query($dbc, $q);
            confirm_query($r, $q);
//            $cats = mysqli_fetch_array($r, MYSQLI_ASSOC);
//            echo "<pre>";
//            print_r($cats);
//            die();
            if(mysqli_num_rows($r)>1){
            while ($pages = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
                echo "
                    <tr>
                <td>{$pages['page_name']}</td>
                    <td>{$pages['date']}</td>
                    <td>{$pages['name']}</td>
                <td>{$pages['content']}</td>                            
                <td><a class='edit' href='edit_page.php?pid={$pages['page_id']}'>Edit</a></td>            
                <td><a class='delete' href='delete_page.php?pid={$pages['page_id']}&pn={$pages['name']}'>Delete</a></td>            
            </tr>";
            }}
            else{
                //neu khong co page de hien thi thi bao loi hoac noi nguoi dung tao moi page
                $message="<p>Khong co page, co tao moi thi tao nha</p>";
//                echo "<p>Khong co page, co tao moi thi tao nha</p>";
            }
            ?>

        </tbody>
    </table>
</div><!--end content-->

<?php include('../includes/footer.php'); ?>